# Maintainer: Martell Malone <martellmalone@gmail.com>
# Contributor: Biswapriyo Nath <nathbappai@gmail.com>

_realname=wslay
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.1.1
pkgrel=3
pkgdesc="The WebSocket library in C (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/tatsuhiro-t/wslay"
license=("spdx:MIT")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-cunit"
             "${MINGW_PACKAGE_PREFIX}-nettle"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx")
source=("https://github.com/tatsuhiro-t/wslay/archive/release-${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-sphinx.patch")
sha256sums=('7b9f4b9df09adaa6e07ec309b68ab376c0db2cfd916613023b52a47adfda224a'
            '74fc7867c6a1304eb9817460f4dcd3601c614fdf22d9bdf74c31b54000f75b97')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/${_realname}-release-${pkgver}"

  apply_patch_with_msg \
    "001-sphinx.patch"

  autoreconf -fiv
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  ../${_realname}-release-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST}

  # WARNING: Dirty hack due to absence of conf.py in source directory.
  #          As it is required by python-sphinx to build man pages.

  cp "${srcdir}/build-${MSYSTEM}/doc/sphinx/conf.py" \
  "${srcdir}/${_realname}-release-${pkgver}/doc/sphinx/conf.py"

  make
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  make DESTDIR="${pkgdir}" install
}
